import tkinter as tk
import time

class Stopwatch:
    def __init__(self, root):
        self.root = root
        self.root.title("Stopwatch")
        self.start_time = None
        self.elapsed_time = 0
        self.is_running = False

        # 设置字体大小为36
        self.label_font = ('Helvetica', 40)
        self.button_font = ('Helvetica', 14)

        self.label = tk.Label(self.root, text="00:00:00.000", font=self.label_font)
        self.label.pack(expand=True)

        # 创建一个Frame来包含按钮
        self.button_frame = tk.Frame(self.root)
        self.button_frame.pack(fill=tk.X, expand=True)

        self.start_button = tk.Button(self.button_frame, text="Start", command=self.start, font=self.button_font)
        self.start_button.pack(side=tk.LEFT, padx=10, pady=10, fill=tk.X, expand=True)

        self.stop_button = tk.Button(self.button_frame, text="Stop", command=self.stop, font=self.button_font)
        self.stop_button.pack(side=tk.LEFT, padx=10, pady=10, fill=tk.X, expand=True)

        self.reset_button = tk.Button(self.button_frame, text="Reset", command=self.reset, font=self.button_font)
        self.reset_button.pack(side=tk.LEFT, padx=10, pady=10, fill=tk.X, expand=True)

    def start(self):
        if not self.is_running:
            self.is_running = True
            self.start_time = time.time() - self.elapsed_time
            self.update_clock()

    def stop(self):
        if self.is_running:
            self.is_running = False
            self.elapsed_time = time.time() - self.start_time
            self.update_clock()

    def reset(self):
        self.is_running = False
        self.elapsed_time = 0
        self.start_time = None
        self.label.config(text="00:00:00.000")

    def update_clock(self):
        if self.is_running:
            current_time = time.time()
            self.elapsed_time = current_time - self.start_time
        hours, remainder = divmod(self.elapsed_time, 3600)
        minutes, seconds = divmod(remainder, 60)
        milliseconds = int((seconds - int(seconds)) * 1000)
        time_str = '{:02d}:{:02d}:{:06.3f}'.format(int(hours), int(minutes), seconds + milliseconds / 1000)
        self.label.config(text=time_str)
        if self.is_running:
            self.root.after(1, self.update_clock)

def main():
    root = tk.Tk()
    stopwatch = Stopwatch(root)
    root.mainloop()

if __name__ == "__main__":
    main()